#!/bin/bash

function start(){
  pid=`ps -aux | grep cyberspace/manage.py | grep -v grep | awk '{print $2}'`
  if [ -z $pid ]; then
    echo "启动程序！"
    nohup python3 manage.py > /dev/null 2>&1 &
  else
    echo "程序已经运行，进程id为[$pid]，不要重复启动"
  fi
}

function state(){
  pid=`ps -aux | grep cyberspace/manage.py | grep -v grep | awk '{print $2}'`
  if [ -z $pid ]; then
    echo "程序未运行！"
  else
    echo "程序正在运行，进程id为[$pid]"
  fi
}

function stop(){  
  pid=`ps -aux | grep cyberspace/manage.py | grep -v grep | awk '{print $2}'`
  if [ -z $pid ]; then
    echo "程序未运行！"
  else
    echo "杀死进程[$pid]"
    kill -9 $pid
  fi
}

function restart(){
  stop
  sleep 3 
  start
}


case "$1" in
  start)
  start
  ;;

  state)
  state
  ;;
  
  stop)
  stop
  ;;
  
  restart)
  restart
  ;;
   
  *)
  echo $"usage:$0 {start | stop | restart| state }"
  exit 1
esac


